package com.zuxia.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zuxia.code.Code;
import com.zuxia.dto.ShopDto;
import com.zuxia.entity.Group;
import com.zuxia.entity.Order;
import com.zuxia.entity.PageDto;
import com.zuxia.entity.Shop;
import com.zuxia.exception.ZuxiaBusinessException;
import com.zuxia.mapper.GroupMapper;
import com.zuxia.order.OrderOpenFeginApi;
import com.zuxia.param.GroupParam;
import com.zuxia.service.GroupService;
import com.zuxia.service.ShopService;
import com.zuxia.shop.ShopOpenFeignApi;
import com.zuxia.utils.R;
import com.zuxia.utils.RedisUtils;
import com.zuxia.utils.ResultCodeEnum;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoli
 * @since 2024-11-15
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements GroupService {
    @Resource
    private GroupMapper groupMapper;
    @Resource
    private ShopOpenFeignApi shopOpenFeignApi;
    @Resource
    private ShopService shopService;

    @Override
    public boolean addGroup(Group group) throws Exception{
        String groupId= IdUtil.simpleUUID();
        group.setGroupId(groupId);
        R r = shopOpenFeignApi.getShopById(group.getShopId());
        if (!r.getSuccess()){
            throw new ZuxiaBusinessException(ResultCodeEnum.SHOP_NULL);
        }
        ShopDto shopDto = BeanUtil.copyProperties(r.getData().get("shop"), ShopDto.class);
        if(shopDto.getShopNum()<group.getGroupNum()*group.getGroupCondition()){
            throw new ZuxiaBusinessException(ResultCodeEnum.SHOP_NUMBER_IS_EXCEED);
        }
        R r1 = shopOpenFeignApi.upShopNumById(group.getShopId(), shopDto.getShopNum() - group.getGroupNum() * group.getGroupCondition());
        if (!r1.getSuccess()){
            throw new ZuxiaBusinessException(ResultCodeEnum.SHOP_NUMBER_IS_UP);
        }
        group.setShopName(shopDto.getShopName());
        group.setShopImg(shopDto.getShopImg());
        group.setShopType(shopDto.getShopType());
        return groupMapper.insert(group) > 0;
    }

    @Override
    public boolean upGroupStatus(String id,Integer status){
        Group group = new Group();
        group.setGroupId(id);
        group.setGroupState(status);
        return groupMapper.updateById(group) > 0;
    }

    @Override
    public boolean upGroupSuccessNum(String id, Integer sum) throws Exception{
        Group groupById = getGroupById(id);
        groupById.setGroupSuccessNum(groupById.getGroupSuccessNum()+sum);
        return groupMapper.updateById(groupById) > 0;
    }

    @Override
    public Group getGroupById(String id) throws Exception{
        Group group = groupMapper.selectById(id);
        if(group==null){
            throw new ZuxiaBusinessException(ResultCodeEnum.GROUP_NULL);
        }
        return group;
    }

    @Override
    public boolean releaseGroup(String id,Integer status) throws Exception{
        Group group = getGroupById(id);
        if(group.getGroupState()!=2){
            throw new ZuxiaBusinessException(ResultCodeEnum.GROUP_REVIEWER);
        }
        if(group.getGroupReleaseStatus()!=status){
            group.setGroupReleaseStatus(status);
            boolean b = groupMapper.updateById(group)>0;
            if (!b) {
                throw new ZuxiaBusinessException(ResultCodeEnum.GROUP_RELEASE_IS_FALSE);
            }
        }
        return true;
    }

    @Override
    public PageDto<Group> getGroupByPage(GroupParam groupParam) throws Exception {
        LambdaQueryWrapper<Group> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Group::getGroupId,groupParam.getId()).or().like(Group::getShopName,groupParam.getId()).eq(Group::getShopType,groupParam.getShopType()).eq(Group::getGroupState,groupParam.getGroupState()).eq(Group::getGroupReleaseStatus,groupParam.getGroupReleaseStatus());
        Page<Group> groupPage = groupMapper.selectPage(new Page<>(groupParam.getPage(), groupParam.getSize()), wrapper);
        PageDto<Group> groupPageDto = new PageDto<>();
        groupPageDto.setList(groupPage.getRecords());
        groupPageDto.setTotal(groupPage.getTotal());
        return groupPageDto;
    }

    @Resource
    private OrderOpenFeginApi orderOpenFeginApi;

    @Override
    public boolean sendShop(String id) {
            R orderById = orderOpenFeginApi.getOrderById(id);
            if(!orderById.getSuccess()){
                throw new ZuxiaBusinessException(ResultCodeEnum.GROUP_IS_RELEASE_NOT);
            }
            Order order = BeanUtil.copyProperties(orderById.getData().get("order-group-single"), Order.class);

            R r = orderOpenFeginApi.sendShop(id);
            if(!r.getSuccess()){
                if(r.getCode()== 10000){
                    throw new ZuxiaBusinessException(ResultCodeEnum.GROUP_SEND_SHOP);
                }else if(r.getCode()==10002){
                    throw new ZuxiaBusinessException(ResultCodeEnum.GROUP_SEND_SHOP_FALSE);
                }else if(r.getCode()==10001){
                    throw new ZuxiaBusinessException(ResultCodeEnum.GROUP_SEND_SHOP_IS_ERROR);
                }
            }
            Shop byId = shopService.getById(order.getShopId());
            System.out.println(byId.getShopNum());
            boolean b = shopService.upShopNumById(byId.getShopId(), byId.getShopNum() - order.getGroupCondition());
            if (!b) {
                throw new ZuxiaBusinessException(ResultCodeEnum.SHOP_NUMBER_IS_UP);
            }
            return true;
    }
}
